from argparse import Namespace
import eventlet
import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})


@sio.event
def connect(sid, environ):
    print('connect ', sid)
    # 建立连接之后方可以发送消息
    # sio.emit('my_message', {'response': 'my response'})
    sio.start_background_task(push_data, ({"name": "Gao"},))


@sio.event  # (namespace="/data")
def my_data(sid, data):
    print('get client message ', data)


def push_data(data):
    print(data[0])
    sio.emit("my_message", data[0])


@sio.event  # (namespace="/data")
def my_email(sid, data):
    print('get client email ', data)


@sio.event
def disconnect(sid):
    print('disconnect ', sid)


if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 5001)), app)
